package com.ygqh.baby.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.mapper.YgSkuSupplyPriceMapper;
import com.ygqh.baby.model.YgSkuInfo;
import com.ygqh.baby.po.YgSkuSupplyPrice;
import com.ygqh.baby.po.YgSkuSupplyPriceExample;
import com.ygqh.baby.service.YgSkuService;
import com.ygqh.baby.service.YgSkuSupplyPriceService;

@Service
public class YgSkuSupplyPriceServiceImpl implements YgSkuSupplyPriceService {

	@Autowired
	private YgSkuSupplyPriceMapper ygSkuSupplyPriceMapper;
	@Autowired
	private YgSkuService ygSkuService;

	@Override
	public int saveYgSkuSupply(YgSkuSupplyPrice ygSkuSupplyPrice) {
		return ygSkuSupplyPriceMapper.insertSelective(ygSkuSupplyPrice);
	}

	@Override
	public int updateYgSkuSupply(YgSkuSupplyPrice ygSkuSupplyPrice) {
		return ygSkuSupplyPriceMapper.updateByPrimaryKeySelective(ygSkuSupplyPrice);
	}

	@Override
	public Message saveYgSkuSupplyPriceBatch(List<YgSkuSupplyPrice> ygSkuSupplyPriceList) {
		if (CollectionUtils.isNotEmpty(ygSkuSupplyPriceList)) {
			List<YgSkuSupplyPrice> saveList = new ArrayList<>();
			List<YgSkuSupplyPrice> updateList = new ArrayList<>();
			for (YgSkuSupplyPrice ygSkuSupplyPrice : ygSkuSupplyPriceList) {
				if (ygSkuSupplyPrice.getId() == null) {
					saveList.add(ygSkuSupplyPrice);
				} else {
					updateList.add(ygSkuSupplyPrice);
				}
			}
			saveBatch(saveList);
			updateBatch(updateList);
			return Message.success(ygSkuSupplyPriceList);
		}
		return Message.error(ygSkuSupplyPriceList);
	}

	@Override
	public YgSkuSupplyPrice findSkuSupplyPrice(Long skuId, Long supplierId) {
		if (skuId != null && supplierId != null) {
			YgSkuSupplyPriceExample example = new YgSkuSupplyPriceExample();
			example.createCriteria().andSkuIdEqualTo(skuId).andSupplierIdEqualTo(supplierId);
			List<YgSkuSupplyPrice> selectByExample = ygSkuSupplyPriceMapper.selectByExample(example);
			if (CollectionUtils.isNotEmpty(selectByExample)) {
				return selectByExample.get(0);
			}
		}
		return null;
	}

	@Override
	public Message updateAllSkuSupplyPrice(Long supplierId, Boolean discountType, BigDecimal discount, String productName, String productCode) {
		List<YgSkuInfo> list = ygSkuService.getSkuInfoList(new QueryInfo(-1, 0), productName, productCode, supplierId, Boolean.TRUE);
		if (CollectionUtils.isNotEmpty(list)) {
			List<YgSkuSupplyPrice> saveList = new ArrayList<>();
			List<YgSkuSupplyPrice> updateList = new ArrayList<>();
			for (YgSkuInfo ygSkuInfo : list) {
				YgSkuSupplyPrice yssp = new YgSkuSupplyPrice();
				if (discountType) {
					yssp.setSupplyPrice(ygSkuInfo.getSalesPrice().multiply(discount));
				} else {
					yssp.setSupplyPrice(ygSkuInfo.getCostPrice().multiply(discount.add(BigDecimal.valueOf(1))));
				}
				if (ygSkuInfo.getSkuSupplyPriceId() == null) {
					yssp.setSupplierId(supplierId);
					yssp.setSkuId(ygSkuInfo.getSkuId());
					saveList.add(yssp);
				} else {
					yssp.setId(ygSkuInfo.getSkuSupplyPriceId());
					updateList.add(yssp);
				}
			}
			saveBatch(saveList);
			updateBatch(updateList);
			return Message.success(list);
		}
		return Message.error("请稍后重试");
	}

	private int updateBatch(List<YgSkuSupplyPrice> itemList) {
		if (CollectionUtils.isEmpty(itemList)) {
			return 0;
		}
		return ygSkuSupplyPriceMapper.updateBatch(itemList);
	}

	private int saveBatch(List<YgSkuSupplyPrice> itemList) {
		if (CollectionUtils.isEmpty(itemList)) {
			return 0;
		}
		return ygSkuSupplyPriceMapper.insertBatch(itemList);
	}
	
}
